<template>
  <div class="login">
    <div class="login-box">
      <!-- 头像 -->
      <div class="login-avatar">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 登录表单 -->
      <el-form :model="formData" class="login-form" :rules="formRules" ref="formRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
            v-model="formData.username"
          >
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            v-model="formData.password"
          >
          </el-input>
        </el-form-item>
        <!-- 登录与重置 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="loginSubmmit">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      formData: {
        username: 'admin',
        password: '123456'
      },
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6到15个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置时，重置登录表单
    resetForm () {
      this.$refs.formRef.resetFields()
    },
    loginSubmmit () {
      // 点击登录，发起请求前的表单内容预校验
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        const { data: result } = await this.$http.post('login', this.formData)
        console.log(result)
        if (result.meta.status !== 200) {
          return this.$message.error('登录失败！请重新登录')
        }
        this.$message.success('登录成功！即将跳转到主页！')
        // 将登录之后的token保存到客户端的sessionStorage中
        // 项目中除了登录之外的其他API接口，必须在登录之后才能访问
        // token只应在当前网站打开期间生效，所以将token保存在sessionStorage中
        window.sessionStorage.setItem('token', result.data.token)
        // 通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background-color: pink;
  height: 100%;
}
.login-box {
  width: 400px;
  height: 300px;
  border-radius: 5px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  .login-avatar {
    width: 100px;
    height: 100px;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 5px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px;
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border: 1px solid #eee;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 30px;
    box-sizing: border-box;
    .btn {
      display: flex;
      justify-content: flex-end;
    }
  }
}

</style>
